/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Student_function;

import come_true.Student;
import come_true.StudentDaoJDBCImpl;
import come_true.User;
import come_true.UserDao;
import come_true.UserDaoImpl;
import JDBCUTIL.JDBCUtil;
import static Product_function.MainFrame.ll;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author C079
 */
public class JFrame_search extends javax.swing.JFrame {
    List<Student> ll;

    public List<Student> getLl() {
        return ll;
    }

    public void setLl(List<Student> ll) {
        this.ll = ll;
    }
    
    /**
     * Creates new form NewJFrame_find
     */
    public JFrame_search() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton_findsave = new javax.swing.JButton();
        jButton_findcancle = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("方法一：请输入学号");

        jLabel2.setText("方法二：请输入姓名");

        jButton_findsave.setText("确认");
        jButton_findsave.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton_findsaveMousePressed(evt);
            }
        });
        jButton_findsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_findsaveActionPerformed(evt);
            }
        });

        jButton_findcancle.setText("取消");
        jButton_findcancle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_findcancleActionPerformed(evt);
            }
        });

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(67, 67, 67)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jButton_findsave)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton_findcancle))
                                .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_findsave)
                    .addComponent(jButton_findcancle))
                .addGap(45, 45, 45))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_findcancleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_findcancleActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButton_findcancleActionPerformed

    private void jButton_findsaveMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_findsaveMousePressed
        // TODO add your handling code here:
        
        Student s = new Student();
        StudentDaoJDBCImpl students = new StudentDaoJDBCImpl();
        System.out.println("查找学生信息");
        System.out.println(ll);
        System.out.println(this.jTextField1.getText()+"  "+this.jTextField2.getText());
        if(!this.jTextField1.getText().trim().equals("")){
            System.out.println("学号不为空,使用学号查询");
            s=students.findStudentById(Integer.parseInt(this.jTextField1.getText()));
        Vector columnNamesStudent = new Vector();
        columnNamesStudent.add("学号");
        columnNamesStudent.add("姓名");
        columnNamesStudent.add("性别");
        columnNamesStudent.add("注册日期");
        columnNamesStudent.add("数学成绩");
        columnNamesStudent.add("Java成绩");
        columnNamesStudent.add("体育成绩");
        columnNamesStudent.add("总分");

        //StudentDaoJDBCImpl students=new StudentDaoJDBCImpl();
        Vector data2 = new Vector();
        
            Vector dataRow = new Vector();
            dataRow.add(s.getId());
            dataRow.add(s.getName());
            dataRow.add(s.getGender());
            dataRow.add(s.getBirthday());
            dataRow.add(Double.toString(s.getScoreList().get(0).getScore()));
            dataRow.add(Double.toString(s.getScoreList().get(1).getScore()));
            dataRow.add(Double.toString(s.getScoreList().get(2).getScore()));
            dataRow.add(Double.toString(s.getTotalpoint()));
            data2.add(dataRow);
        JTable jTable3 = new JTable(data2, columnNamesStudent);
        jScrollPane2.setViewportView(jTable3);
        }else if(!this.jTextField2.getText().trim().equals("")){
            System.out.println("姓名不为空，使用姓名查询");
            ll=students.findStudentByName(this.jTextField2.getText().trim());
             Vector columnNamesStudent = new Vector();
            columnNamesStudent.add("学号");
            columnNamesStudent.add("姓名");
            columnNamesStudent.add("性别");
            columnNamesStudent.add("注册日期");
            columnNamesStudent.add("数学成绩");
            columnNamesStudent.add("Java成绩");
            columnNamesStudent.add("体育成绩");
            columnNamesStudent.add("总分");
        Vector data2 = new Vector();

        //List<Student> ll=students.diplayAllStudent();
        if (ll == null) {
            ll = students.findStudentByName(this.jTextField2.getText());
        }
        for (int i = 0; i < ll.size(); i++) {
            Vector dataRow = new Vector();
            dataRow.add(ll.get(i).getId());
            dataRow.add(ll.get(i).getName());
            dataRow.add(ll.get(i).getGender()); 
            dataRow.add(ll.get(i).getBirthday());
            dataRow.add(Double.toString(ll.get(i).getScoreList().get(0).getScore()));
            dataRow.add(Double.toString(ll.get(i).getScoreList().get(1).getScore()));
            dataRow.add(Double.toString(ll.get(i).getScoreList().get(2).getScore()));
            dataRow.add(Double.toString(ll.get(i).getTotalpoint()));
            data2.add(dataRow);
        }
        JTable jTable3 = new JTable(data2, columnNamesStudent);
        jScrollPane2.setViewportView(jTable3);
        }else{
            System.out.println("两个都是空，无法查询");
        }
        
    }//GEN-LAST:event_jButton_findsaveMousePressed

    private void jButton_findsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_findsaveActionPerformed
        // TODO add your handling code here:
        
    }
    private javax.swing.JButton jButton_findcancle;
    private javax.swing.JButton jButton_findsave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
